<template>
  <el-row :gutter="20">
    <el-col :span="4">
      <category @tree-node-click="treeNodeClick"></category>
    </el-col>
    <el-col :span="19">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="组名" prop="attrGroupName">
          <el-input
            v-model="queryParams.attrGroupName"
            placeholder="请输入组名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['product:attrgroup:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['product:attrgroup:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['product:attrgroup:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['product:attrgroup:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="attrgroupList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="分组id" align="center" prop="attrGroupId"/>
        <el-table-column label="组名" align="center" prop="attrGroupName"/>
        <el-table-column label="排序" align="center" prop="sort"/>
        <el-table-column label="描述" align="center" prop="descript"/>
        <el-table-column label="组图标" align="center" prop="icon">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column label="所属分类" align="center" prop="category.name"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-grid"
              @click="handleGroupAttr(scope.row)"
              v-hasPermi="['product:attrgroup:edit']"
            >关联参数</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['product:attrgroup:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['product:attrgroup:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改属性分组对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="组名" prop="attrGroupName">
            <el-input v-model="form.attrGroupName" placeholder="请输入组名"/>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序"/>
          </el-form-item>
          <el-form-item label="描述" prop="descript">
            <el-input v-model="form.descript" placeholder="请输入描述"/>
          </el-form-item>
          <el-form-item label="组图标" prop="icon">
            <!--<el-input v-model="form.icon" placeholder="请输入组图标"/>-->
            <el-popover
              placement="bottom-start"
              width="460"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
            >
              <IconSelect ref="iconSelect" @selected="selected" />
              <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                <svg-icon
                  v-if="form.icon"
                  slot="prefix"
                  :icon-class="form.icon"
                  class="el-input__icon"
                  style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
          <el-form-item label="所属分类" prop="categoryId">
            <el-cascader style="width: 380px"
              v-model="form.categoryId"
              :options="categoryOptions" :props="categoryProps"></el-cascader>
            <!--<el-input v-model="form.categoryId" placeholder="请输入所属分类"/>-->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>

</template>

<script>
import {listCategory} from "@/api/product/category";
import {addAttrgroup, delAttrgroup, getAttrgroup, listAttrgroup, updateAttrgroup} from "@/api/product/attrgroup";
import Category from "../../components/category";
import IconSelect from "@/components/IconSelect";

export default {
    name: "Attrgroup",
    components: {Category,IconSelect},
    data() {
      return {
        categoryProps:
          {
            value:'catId',
            label:'name',
            children:'children'
          },
        categoryOptions:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 属性分组表格数据
        attrgroupList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          attrGroupName: null,
          categoryId:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          attrGroupName: [
            { required: true, message: "组名不能为空", trigger: "blur" }
          ],
          sort: [
            { required: true, message: "排序不能为空", trigger: "blur" }
          ],
          descript: [
            { required: true, message: "描述不能为空", trigger: "blur" }
          ],
          categoryId: [
            { required: true, message: "所属分类id不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getCategories();
    },
    methods: {
      handleGroupAttr(row){
        this.$router.push("/product/attrgroup-auth/attr/" + row.attrGroupId);
      },
      getCategories(){
        listCategory().then(response => {
          this.categoryOptions = this.handleTree(response.data, "catId", "parentCid");
        });
      },
      // 选择图标
      selected(name) {
        this.form.icon = name;
      },
      treeNodeClick(data,node,component){
        //console.log(data,node,component)
        if(node.level==3){
          this.queryParams.categoryId=data.catId;
          this.getList();
        }
      },
      /** 查询属性分组列表 */
      getList() {
        this.loading = true;
        listAttrgroup(this.queryParams).then(response => {
          this.attrgroupList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          attrGroupId: null,
          attrGroupName: null,
          sort: null,
          descript: null,
          icon: null,
          categoryId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.attrGroupId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加属性分组";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const attrGroupId = row.attrGroupId || this.ids
        getAttrgroup(attrGroupId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改属性分组";
        });
      },
      /** 提交按钮 */
      submitForm() {

        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.attrGroupId != null) {
              updateAttrgroup(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.categoryId=this.form.categoryId[this.form.categoryId.length-1];
              addAttrgroup(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const attrGroupIds = row.attrGroupId || this.ids;
        this.$modal.confirm('是否确认删除属性分组编号为"' + attrGroupIds + '"的数据项？').then(function() {
          return delAttrgroup(attrGroupIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('product/attrgroup/export', {
          ...this.queryParams
        }, `attrgroup_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style scoped>
  .el-row {
    left: 20px;
    /*top: 20px;*/
    margin-top: 20px;
  }

</style>
